// const cityJson = require('../../config/city');
const { City } = require('../../models/ctcdb');
const _ = require('lodash');
async function getList() {
  // return { list: cityJson }
  const list = await City.findAll({
    attributes: ['type','provinceId','cityId','areaId','provinceName','cityName','areaName'],
    raw: true
  });
  let cityMap = {};
  let provinceMap = {};
  let map = {};
  list.forEach((item) => {
    if (item.type === 3) {
      if (!cityMap[item.cityId]) {
        cityMap[item.cityId] = [];
      }
      cityMap[item.cityId].push({
        name: item.areaName,
        id: item.areaId
      })
    }
    if (item.type === 2) {
      if (!provinceMap[item.provinceId]) {
        provinceMap[item.provinceId] = [];
      }
      provinceMap[item.provinceId].push({
        name: item.cityName,
        id: item.cityId
      })
    }
    if (item.type === 1) {
      if (!map[item.provinceId]) {
        map[item.provinceId] = {
          cityList: [],
          name: item.provinceName,
          id: item.provinceId,
        }
      }
    }
  });
  for (let i in provinceMap) {
    provinceMap[i].forEach((item) => {
      if (cityMap[item.id]) {
        if (map[i]) {
          map[i].cityList.push({
            areaList: cityMap[item.id],
            name: item.name,
            id: item.id
          })
        }
      }
    })
  }
  const arr = [];
  for (let i in map) {
    arr.push(map[i]);
  }
  return { list: arr };
}

module.exports = {
  getList
};
